package com.practice.springboot_01_zwj.service.impl;

import com.practice.springboot_01_zwj.entity.ProjectReport;
import com.practice.springboot_01_zwj.entity.ReportDetails;
import com.practice.springboot_01_zwj.mapper.ProjectReportMapper;
import com.practice.springboot_01_zwj.mapper.ReportDetailsMapper;
import com.practice.springboot_01_zwj.service.ProjectReportService;
import com.practice.springboot_01_zwj.service.ReportDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ReportDetailsServiceImpl implements ReportDetailsService {

    @Autowired
    private ProjectReportMapper projectReportMapper;

    @Autowired
    private ReportDetailsMapper reportDetailsMapper;

    @Override
    public ProjectReport getProjectReportByKeyId(Integer keyId) {
        return projectReportMapper.selectByKeyId(keyId);
    }

    @Override
    public List<ReportDetails> getReportDetailsByKeyId(Integer keyId) {
        return reportDetailsMapper.selectByKeyId(keyId);
    }

    @Override
    public ReportDetails getReportDetailById(Integer rid) {
        return reportDetailsMapper.selectByRid(rid);
    }

    @Override
    public int addReportDetail(ReportDetails reportDetails) {
        // 1. 验证计划开始时间和结束时间的顺序
        if (reportDetails.getPlanEndTime() != null && reportDetails.getPlanBeginTime() != null) {
            if (reportDetails.getPlanEndTime().before(reportDetails.getPlanBeginTime())) {
                throw new IllegalArgumentException("计划结束时间不能早于计划开始时间");
            }
        }

        // 2. 获取一级菜单的项目时间范围
        ProjectReport projectReport = projectReportMapper.selectByKeyId(reportDetails.getKeyId());
        if (projectReport == null) {
            throw new IllegalArgumentException("关联的一级菜单不存在");
        }

        // 3. 验证二级菜单时间是否在一级菜单时间范围内
        if (reportDetails.getPlanBeginTime() != null && projectReport.getProjectBeginTime() != null) {
            if (reportDetails.getPlanBeginTime().before(projectReport.getProjectBeginTime())) {
                throw new IllegalArgumentException("计划开始时间不能早于项目开始时间");
            }
        }

        if (reportDetails.getPlanEndTime() != null && projectReport.getProjectEndTime() != null) {
            if (reportDetails.getPlanEndTime().after(projectReport.getProjectEndTime())) {
                throw new IllegalArgumentException("计划结束时间不能晚于项目结束时间");
            }
        }

        return reportDetailsMapper.insert(reportDetails);
    }

    @Override
    public int updateReportDetail(ReportDetails reportDetails) {
        // 获取原有记录
        ReportDetails existingDetail = reportDetailsMapper.selectByRid(reportDetails.getRid());
        if (existingDetail == null) {
            throw new IllegalArgumentException("要更新的记录不存在");
        }

        // 确定要使用的keyId（如果传入的keyId为null，使用原有的keyId）
        Integer keyId = reportDetails.getKeyId() != null ? reportDetails.getKeyId() : existingDetail.getKeyId();

        // 1. 验证计划开始时间和结束时间的顺序
        Date planBeginTime = reportDetails.getPlanBeginTime() != null ? reportDetails.getPlanBeginTime() : existingDetail.getPlanBeginTime();
        Date planEndTime = reportDetails.getPlanEndTime() != null ? reportDetails.getPlanEndTime() : existingDetail.getPlanEndTime();

        if (planEndTime != null && planBeginTime != null) {
            if (planEndTime.before(planBeginTime)) {
                throw new IllegalArgumentException("计划结束时间不能早于计划开始时间");
            }
        }

        // 2. 获取一级菜单的项目时间范围
        ProjectReport projectReport = projectReportMapper.selectByKeyId(keyId);
        if (projectReport == null) {
            throw new IllegalArgumentException("关联的一级菜单不存在");
        }

        // 3. 验证二级菜单时间是否在一级菜单时间范围内
        if (planBeginTime != null && projectReport.getProjectBeginTime() != null) {
            if (planBeginTime.before(projectReport.getProjectBeginTime())) {
                throw new IllegalArgumentException("计划开始时间不能早于项目开始时间");
            }
        }

        if (planEndTime != null && projectReport.getProjectEndTime() != null) {
            if (planEndTime.after(projectReport.getProjectEndTime())) {
                throw new IllegalArgumentException("计划结束时间不能晚于项目结束时间");
            }
        }

        return reportDetailsMapper.update(reportDetails);
    }

    @Override
    public int deleteReportDetail(Integer rid) {
        return reportDetailsMapper.deleteByRid(rid);
    }

    @Override
    public int deleteReportDetailsByKeyId(Integer keyId) {
        return reportDetailsMapper.deleteByKeyId(keyId);
    }
}